package org.chen.pipeline;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.chen.common.constant.ArticleStatusEnum;
import org.chen.common.utils.SnowflakeIdGenerator;
import org.chen.exception.CrawlerException;
import org.chen.mapper.ArticleRepository;
import org.chen.domain.Article;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

// 结果收集Pipeline
@Slf4j
@RequiredArgsConstructor
public class ResultCollectorPipeline implements Pipeline {
    private final ArticleRepository articleRepository;
    private final List<Article> articles;
    private final List<String> errors;
    private final AtomicInteger success;
    private final AtomicInteger failed;

    @Override
    public void process(ResultItems resultItems, Task task) {
        try {
            Article article = resultItems.get("article");
            if (article != null) {
                Article savedArticle = saveArticle(article);
                if (savedArticle != null) {
                    articles.add(savedArticle);
                    success.incrementAndGet();
                }
            }
        } catch (Exception e) {
            log.error("Error processing article: {}", e.getMessage());
            errors.add("Error processing article: " + e.getMessage());
            failed.incrementAndGet();
        }
    }
    
    private Article saveArticle(Article article) {
        try {
            Article existingArticle = articleRepository.selectOne(
                new LambdaQueryWrapper<Article>()
                    .eq(Article::getUrl, article.getUrl())
            );

            if (existingArticle != null) {
                log.info("Article with URL {} already exists", article.getUrl());
                return null;
            } else {
                article.setId(SnowflakeIdGenerator.nextId());
                article.setStatus(ArticleStatusEnum.PENDING.getCode());
                article.setVersion(1);
                articleRepository.insert(article);
                return article;
            }
        } catch (Exception e) {
            log.error("Error saving article: {}", e.getMessage());
            throw new CrawlerException("Failed to save article: " + e.getMessage(), e);
        }
    }
}